
/**
 * @file XYXKC
 * @brief XYXKC's ESPONE Mind+ library.
 * @n this is the iot module of xiaoyingxing
 *
 * @copyright    XYXKC,2024
 * @copyright    MIT Lesser General Public License
 *
 * @author [email](hyc@xyxedu.cn)
 * @date  2024-8-27
 */
enum RECONNECT {
    //% block="startUp"
    true,
    //% block="close"
    false,

}
enum TOPICNUMBER {
    //% block="topic_0"
    0,
    //% block="topic_1"
    1,
    //% block="topic_2"
    2,
    //% block="topic_3"
    3,
    //% block="topic_4"
    4,
    //% block="topic_5"
    5,
    //% block="topic_6"
    6,
    //% block="topic_7"
    7,
    //% block="topic_8"
    8,
    //% block="topic_9"
    9,
    //% block="topic_10"
    10,
    //% block="topic_11"
    11,
    //% block="topic_12"
    12,
    //% block="topic_13"
    13,
    //% block="topic_14"
    14,
    //% block="topic_15"
    15,
    //% block="topic_16"
    16,
    //% block="topic_17"
    17,
    //% block="topic_18"
    18,
    //% block="topic_19"
    19,

}
//% color="#FFB366" iconWidth=50 iconHeight=40
namespace XYXKCIOT {
    /***
    //% block="[RECONNECT]Wi-Fi disconnect and reconnect" blockType="command"
    //% RECONNECT.shadow="dropdown" RECONNECT.options="RECONNECT" RECONNECT.defl="RECONNECT.true"
    export function configWifiInit(parameter: any, block: any) {
        let Reconnect = parameter.RECONNECT.code;
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addSetup('XYXKC_IOT_WIFI_Setup',`xyxkcIot.setReconnectWIFI(${Reconnect});`)
    }
***/
    //% block="WI-FI connection to hotspot name：[NAME] password：[PASS]" blockType="command"
    //% NAME.shadow="String" NAME.defl="ssid"
    //% PASS.shadow="String" PASS.defl="password"
    export function stopOfPatrolCar(parameter: any, block: any) {
        let name = parameter.NAME.code;
        let pass = parameter.PASS.code;
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addCode(`xyxkcIot.WiFiConnection(${name},${pass});`)
    }
    //% block="WI-FI connection successful?" blockType="boolean"
    export function isWiFiConnected(parameter: any, block: any) {
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addCode(`xyxkcIot.isWiFiConnected()`);
    }
    /***
    //% block="WI-FI Set AP Mode Hotspot Name：[NAME] password：[PASS]" blockType="command"
    //% NAME.shadow="String" NAME.defl="apSsid"
    //% PASS.shadow="String" PASS.defl="apPassword"
    export function WiFiHotSpot(parameter: any, block: any) {
        let name = parameter.NAME.code;
        let pass = parameter.PASS.code;
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addCode(`xyxkcIot.WiFiHotSpot(${name},${pass});`)
    }

    //% block="[RECONNECT]mqtt disconnected and reconnected" blockType="command"
    //% RECONNECT.shadow="dropdown" RECONNECT.options="RECONNECT" RECONNECT.defl="RECONNECT.true"
    export function configMQTTInit(parameter: any, block: any) {
        let Reconnect = parameter.RECONNECT.code;
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addSetup('XYXKC_IOT_MQTT_Setup',`xyxkcIot.setReconnectMQTT(${Reconnect});`)
    }
     ***/
    //% block="initialize mqtt account：[NAME] password：[PASS] " blockType="command"
    //% NAME.shadow="String" NAME.defl="mqttName"
    //% PASS.shadow="String" PASS.defl="mqttPass"
    export function configureMQTT(parameter: any, block: any) {
        let name = parameter.NAME.code;
        let pass = parameter.PASS.code;
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addSetup('xyxkcIot_addSetup',`xyxkcIot.configureMQTT(${name},${pass});`)
    }
    //% block="MQTT initiates connection" blockType="command"
    export function connectMQTT(parameter: any, block: any) {
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addCode(`xyxkcIot.connectMQTT();`);
    }
    //% block="MQTT connection successful?" blockType="boolean"
    export function isMQTTConnected(parameter: any, block: any) {
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addCode(`xyxkcIot.isMQTTConnected()`);
    }
    //% block="subscription theme[TOPIC]" blockType="command"
    //% TOPIC.shadow="String" TOPIC.defl="xyxkc/topic"
    export function subscribeTopic(parameter: any, block: any) {
        let topic = parameter.TOPIC.code;
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addCode(`xyxkcIot.subscribeTopic(${topic});`);
    }

    //% block="When received[TOPIC]Message from MQTT" blockType="hat"
    //% TOPIC.shadow="dropdown" TOPIC.options="TOPICNUMBER" TOPIC.defl="TOPICNUMBER.0"
    export function setCustomMqttCallbackMsgNumber(parameter: any, block: any) {
        let topic = parameter.TOPIC.code;
        let funcName = `obloqMqttEventT${topic.replace(/"/g,"").replace(/\//g,"47")}`
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addSetup(`${funcName}`, `xyxkcIot.setCustomMqttCallback(${funcName},${topic});`);
        Generator.addEvent(`${funcName}`, "void", `${funcName}`, "String& message", true);
    }

    //% block="MQTT message" blockType="reporter"
    export function MqttMsg(parameter: any, block: any) {

        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addCode("message");

    }

    function getRandomString(len){
        let _charStr = 'abacdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789',
            min = 0,
            max = _charStr.length-1,
            _str = '';                    //定义随机字符串 变量
        //判断是否指定长度，否则默认长度为15
        len = len || 15;
        //循环生成字符串
        for(var i = 0, index; i < len; i++){
            index = (function(randomIndexFunc, i){
                return randomIndexFunc(min, max, i, randomIndexFunc);
            })(function(min, max, i, _self){
                let indexTemp = Math.floor(Math.random()*(max-min+1)+min),
                    numStart = _charStr.length - 10;
                if(i==0&&indexTemp >=numStart){
                    indexTemp = _self(min, max, i, _self);
                }
                return indexTemp ;
            }, i);
            _str += _charStr[index];
        }
        return _str;
    }
    let contu = 0

    //% block="When received[TOPIC]The message is[MSG]" blockType="hat"
    //% TOPIC.shadow="dropdown" TOPIC.options="TOPICNUMBER" TOPIC.defl="TOPICNUMBER.0"
    //% MSG.shadow="String" MSG.defl="hello"
    export function setCustomMqttCallbackNumber(parameter: any, block: any) {
        let topic = parameter.TOPIC.code;
        let msg = parameter.MSG.code;
        let funcName = `whent${topic.replace(/"/g,"")}Received`
        // let id = `${getRandomString(15)}x${Math.floor(Math.random() * 100)}y${getRandomString(30)}`
        let id = `${contu}`
        contu++
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addSetup(`${funcName}_${id}`, `xyxkcIot.setCustomMqttCallback(${topic},${msg},${funcName}_${id});`);
        Generator.addEvent(`${funcName}_${id}`, "void", `${funcName}_${id}`, "", true);
    }


    //% block="MQTT sending [MSG]Message to[TOPIC]" blockType="command"
    //% TOPIC.shadow="dropdown" TOPIC.options="TOPICNUMBER" TOPIC.defl="TOPICNUMBER.0"
    //% MSG.shadow="String" MSG.defl="hello"

    export function publishNumberZero(parameter: any, block: any) {
        let topic = parameter.TOPIC.code;
        let msg = parameter.MSG.code;
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addCode(`xyxkcIot.publishMessage(${topic},String(${msg}),0);`);

    }
    //% block="MQTT sending [MSG]Message to[TOPIC]coexistenceDatabase（SIOT V2）" blockType="command"
    //% TOPIC.shadow="dropdown" TOPIC.options="TOPICNUMBER" TOPIC.defl="TOPICNUMBER.0"
    //% MSG.shadow="String" MSG.defl="hello"

    export function publishNumberOne(parameter: any, block: any) {
        let topic = parameter.TOPIC.code;
        let msg = parameter.MSG.code;
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addCode(`xyxkcIot.publishMessage(${topic},String(${msg}),1);`);

    }
/***
    //% block="MQTT connection status" blockType="reporter"
    export function mqttState(parameter: any, block: any) {
        Generator.addInclude('XYXKC_IOT_Include','#include <XYXKC_IOT.h>')
        Generator.addObject('XYXKC_IOT_object','XYXKC_IOT','xyxkcIot')
        Generator.addCode(`xyxkcIot.mqttState()`);

    }***/
}
